package cn.kgc.controller;

import cn.kgc.base.entity.CartSkuInfo;
import cn.kgc.base.vo.SecKillSkuInfoVo;
import cn.kgc.dto.SpuInfoVO;
import cn.kgc.entity.*;
import cn.kgc.service.*;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import cn.kgc.dto.PmsSkuInfoAddDTO;
import cn.kgc.dto.PmsSkuInfoUpdateDTO;
import io.swagger.annotations.ApiImplicitParams;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiImplicitParam;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.annotation.Validated;
import cn.kgc.commons.vo.Result;
import com.baomidou.mybatisplus.core.metadata.IPage;
import cn.kgc.entity.PmsSkuInfo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

/**
 * sku信息(PmsSkuInfo)表控制层
 *
 * @author makejava
 * @since 2022-12-05 09:38:35
 */
@RestController
@RequestMapping("/pms/SkuInfo")
@Api
@Validated
public class PmsSkuInfoController {
    /**
     * 服务对象
     */
    @Resource
    private PmsSkuInfoService pmsSkuInfoService;

    @Resource
    private PmsBrandService pmsBrandService;

    @Resource
    private PmsCategoryService pmsCategoryService;

    @Resource
    private PmsSpuInfoService pmsSpuInfoService;

    @Resource
    private PmsSkuSaleAttrValueService pmsSkuSaleAttrValueService;




    @GetMapping("/detail/{skuId}")
    @ApiOperation(value = "查看商品详情信息",httpMethod = "GET",
            produces = "application/json;charset=utf-8",response = Result.class)
    @CrossOrigin
    @ApiImplicitParam(name = "skuId",value = "skuId",paramType = "path",dataType = "long")
    public SecKillSkuInfoVo detail1(@PathVariable("skuId") Long skuId){
        PmsSkuInfo pmsSkuInfo = pmsSkuInfoService.getById(skuId);
        LambdaQueryWrapper<PmsSkuSaleAttrValue>wrapper1=new LambdaQueryWrapper<>();
        wrapper1.eq(PmsSkuSaleAttrValue::getSkuId,skuId);
        List<PmsSkuSaleAttrValue> pmsSkuSaleAttrValues = pmsSkuSaleAttrValueService.list(wrapper1);
        String salAttrValues="";
        for (PmsSkuSaleAttrValue pmsSkuSaleAttrValue : pmsSkuSaleAttrValues) {
            salAttrValues+=pmsSkuSaleAttrValue.getAttrName()+":"+pmsSkuSaleAttrValue.getAttrValue()+"";
        }
        SecKillSkuInfoVo secKillSkuInfoVo=new SecKillSkuInfoVo();
        secKillSkuInfoVo.setDefaultImage(pmsSkuInfo.getSkuDefaultImg());
        secKillSkuInfoVo.setSaleAttrs(salAttrValues);
        secKillSkuInfoVo.setSkuName(pmsSkuInfo.getSkuName());
        return secKillSkuInfoVo;
    }


    /**
    * 给购物车模块提供商品信息接口
    */
    @GetMapping("/getCart")
    @ApiOperation(value = "给购物车模块提供商品信息接口", produces = "application/json;charset=utf-8",
            httpMethod = "GET", response = Result.class)
    @ApiImplicitParam(name = "skuId", value = "skuId", dataType = "Long", paramType = "query")
    public CartSkuInfo getCartSkuInfo(Long skuId){
        PmsSkuInfo pmsSkuInfo = pmsSkuInfoService.getById(skuId);
        Long spuId = pmsSkuInfo.getSpuId();
        String defaultImg = pmsSkuInfo.getSkuDefaultImg();
        BigDecimal price = pmsSkuInfo.getPrice();
        String title = pmsSkuInfo.getSkuTitle();
        //根据skuId获得销售属性的值
        LambdaQueryWrapper<PmsSkuSaleAttrValue> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(PmsSkuSaleAttrValue::getSkuId,skuId);
        List<PmsSkuSaleAttrValue> pmsSkuSaleAttrValueList =
                pmsSkuSaleAttrValueService.list(queryWrapper);
        //将查询出来的销售属性进行转换，转换为字符串 属性名：属性值
        List<String> attrs = pmsSkuSaleAttrValueList.stream().map(pmsSkuSaleAttrValue -> {
            String attr = pmsSkuSaleAttrValue.getAttrName() + ":" + pmsSkuSaleAttrValue.getAttrValue();
            return attr;
        }).collect(Collectors.toList());

        CartSkuInfo cartSkuInfo = CartSkuInfo.builder().skuId(skuId)
                .spuId(spuId).image(defaultImg).title(title).skuAttr(attrs).price(price).build();
        return  cartSkuInfo ;
    }



    /**
     * 分页查询所有数据
     *
     * @param current 分页对象
     * @return 所有数据
     */
    @GetMapping("/list")
    @ApiOperation(value = "分页查询sku数据", produces = "application/json;charset=utf-8",
            httpMethod = "GET", response = Result.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current", value = "当前页", paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "pageSize", value = "每页显示条数,默认5条", paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "skuName", value = "属性的名字", paramType = "query", dataType = "string"),
            @ApiImplicitParam(name = "catalogId", value = "分类的Id,默认值是0", paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "brandId", value = "品牌的Id,默认值是0", paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "spuId", value = "SpuId,默认值是0", paramType = "query", dataType = "int")

    })
    @CrossOrigin
    public IPage<PmsSkuInfo> selectAll(@RequestParam(defaultValue = "1") int current,
                                       @RequestParam(defaultValue = "5") int pageSize,
                                       @RequestParam(defaultValue = "0")int spuId,
                                       @RequestParam(defaultValue = "")String skuName,
                                       @RequestParam(defaultValue = "0")int catalogId,
                                       @RequestParam(defaultValue = "0")int brandId) {
        LambdaQueryWrapper<PmsSkuInfo>lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(spuId!=0,PmsSkuInfo::getSpuId,spuId);
        lambdaQueryWrapper.like(StringUtils.isNotEmpty(skuName),PmsSkuInfo::getSkuName,skuName);
        lambdaQueryWrapper.eq(catalogId!=0,PmsSkuInfo::getCatalogId,catalogId);
        lambdaQueryWrapper.eq(brandId!=0,PmsSkuInfo::getBrandId,brandId);
        IPage<PmsSkuInfo> page = new Page(current, pageSize);
        pmsSkuInfoService.page(page,lambdaQueryWrapper);
        List<PmsSkuInfo> skuInfoList = page.getRecords();
        for (PmsSkuInfo pmsSkuInfo : skuInfoList) {
            Long brandIdTemp = pmsSkuInfo.getBrandId();
            Long catalogTemp = pmsSkuInfo.getCatalogId();
            Long spuIdTemp = pmsSkuInfo.getSpuId();
            if (brandIdTemp!=null){
                PmsBrand pmsBrand = pmsBrandService.getById(brandIdTemp);
                pmsSkuInfo.setBrandName(pmsBrand.getName());
            }
            if (catalogTemp!=null){
                PmsCategory pmsCategory = pmsCategoryService.getById(catalogTemp);
                pmsSkuInfo.setCategoryName(pmsCategory.getName());
            }

            if (spuIdTemp!=null){
                PmsSpuInfo pmsSpuInfo = pmsSpuInfoService.getById(spuIdTemp);
                pmsSkuInfo.setSpuName(pmsSpuInfo.getSpuName());
            }


        }

        return page;
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    @CrossOrigin
    @ApiOperation(value = "通过主键查询单条数据", produces = "application/json;charset=utf-8",
            httpMethod = "GET", response = Result.class)
    @ApiImplicitParam(name = "id", value = "主键", paramType = "path", dataType = "int")
    public PmsSkuInfo selectOne(@PathVariable Serializable id) {
        return this.pmsSkuInfoService.getById(id);
    }



    /**
     * 修改数据
     *
     * @param pmsSkuInfoDTO 实体对象
     * @return 修改结果
     */
    @PostMapping("/update")
    @ApiOperation(value = "修改信息", produces = "application/json;charset=utf-8",
            httpMethod = "POST", response = Result.class)
    @CrossOrigin
    public boolean update(@RequestBody PmsSkuInfoUpdateDTO pmsSkuInfoDTO) {
        PmsSkuInfo pmsSkuInfo = this.pmsSkuInfoService.getById(pmsSkuInfoDTO.getId());
        BeanUtils.copyProperties(pmsSkuInfoDTO, pmsSkuInfo);
        return this.pmsSkuInfoService.updateById(pmsSkuInfo);
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除结果
     */
    @DeleteMapping("/del/{id}")
    @ApiOperation(value = "根据主键删除信息", produces = "application/json;charset=utf-8",
            httpMethod = "DELETE", response = Result.class)
    @ApiImplicitParam(name = "id", value = "主键", paramType = "path", dataType = "int")
    @CrossOrigin
    public boolean delete(@PathVariable Long id) {
        return this.pmsSkuInfoService.removeById(id);
    }
}
